package vip.george.p0009;

public class P9 {
    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        String s = String.valueOf(x);
        int left = 0;
        int right = s.length() -1;
        // boolean result = true;
        while(left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }


    public boolean isPalindrome2(int x) {
        if (x < 0) {
            return false;
        }
        String s = String.valueOf(x);
        String s_reverse = new StringBuilder(s).reverse().toString();
        return s.equals(s_reverse);
    }


    public boolean isPalindrome3(int x) {
        if (x < 0) {
            return false;
        }
        // 使用long 型，避免反转后的数值溢出
        long reverse = 0;
        int temp = x;
        while (temp != 0) {
            reverse = reverse * 10 + temp % 10;
            temp = temp / 10;
        }
        return reverse == x;
    }

    public static void main(String[] args) {
        P9 p9 = new P9();
        System.out.println(p9.isPalindrome3(121));
        System.out.println(p9.isPalindrome3(-121));
        System.out.println(p9.isPalindrome3(10));
        System.out.println(p9.isPalindrome3(11011));
    }
}
